package com.tiantianfresh.admin.controller;

import com.tiantianfresh.admin.service.CommodityService;
import com.tiantianfresh.model.pojo.AJAXResult;
import com.tiantianfresh.model.pojo.Commodity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/commodity")
public class CommodityController {

    @Autowired
    private CommodityService commodityService;

    @RequestMapping("/index/{crid}")
    public String index(@PathVariable Integer crid,Model model){
        model.addAttribute("crid",crid);
        return "commodity/index";
    }
    //查询全部Commodity
    @ResponseBody
    @RequestMapping("/allCommodity/{crid}")
    public Object list(@PathVariable Integer crid){
        List<Commodity> list=commodityService.selectAll(crid);
        return list;
    }
    //跳转到添加Commodity页面
    @RequestMapping("/toadd")
    public String add(){
        return "add";
    }
    //添加Commodity
    @ResponseBody
    @RequestMapping("/add")
    public Object add(Commodity commodity){
        AJAXResult result = new AJAXResult();
        result.setSuccess(commodityService.addCommodity(commodity));
        return result;
    }
    //跳转到修改书籍页面
    @RequestMapping("/toupdate/{id}")
    public String update(@PathVariable int id, Model model){
        Commodity commodity =commodityService.queryCommodityById(id);
        model.addAttribute("Commodity",commodity);
        return "update";
    }
    //保存修改信息
    @ResponseBody
    @RequestMapping("/update")
    public Object update(Commodity commodity){
        AJAXResult result = new AJAXResult();
        result.setSuccess(commodityService.updateCommodity(commodity));
        return result;
    }
    //删除书籍
    @ResponseBody
    @RequestMapping("/del")
    public Object del(@PathVariable Integer id){
        AJAXResult result = new AJAXResult();
        result.setSuccess(commodityService.deleteCommodityById(id));
        return result;
    }
}
